﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;    

namespace School.Admission
{
    public class WorkAddress
    {
        private static SchoolEntities db = new SchoolEntities();

        public static long Add(SD_WorkAddress workaddress)
        {
            try
            {
                db.AddToSD_WorkAddress(workaddress);
                db.SaveChanges();
                return workaddress.Id;

            }

            catch
            {
                return 0;

            }
        }

        public static SD_WorkAddress Single(long id)
        {
            return db.SD_WorkAddress.Single(x => x.Id == id);
        }


        public static SD_WorkAddress SingleByFatherId(long fid)
        {
            try
            {
                return db.SD_WorkAddress.Single(x => x.Id == fid);
            }
            catch
            {
                return null;
            }
        }



        public static IEnumerable<SD_WorkAddress> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from homeaddress in db.SD_WorkAddress select homeaddress;

            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "CompanyName")
                {
                    query = query.Where(c => c.CompanyName == item.Value);
                }

                if (item.Key == "StreetName")
                {
                    query = query.Where(c => c.StreetName == item.Value);
                }


            }
            return query.ToList();
        }

        public static void Update(SD_WorkAddress homeaddress)
        {
            SD_WorkAddress c = db.SD_WorkAddress.Single(x => x.Id == homeaddress.Id);


            c.Id = homeaddress.Id;
            c.Career=homeaddress.Career;
            c.CompanyName=homeaddress.CompanyName;
            c.BuildingNo = homeaddress.BuildingNo;
            c.FloorNo = homeaddress.FloorNo;
            c.FlatNo = homeaddress.FlatNo;
            c.StreetName = homeaddress.StreetName;
            c.Stat_Id = homeaddress.Stat_Id;
            c.District_Id = homeaddress.District_Id;
            c.PostCode = homeaddress.PostCode;
            c.Phone1 = homeaddress.Phone1;
            c.Phone2 = homeaddress.Phone2;
            c.Phone3 = homeaddress.Phone3;
            c.InternalPhoneExtension = homeaddress.InternalPhoneExtension;
            c.FAX = homeaddress.FAX;
            c.Mobile1 = homeaddress.Mobile1;
            c.Mobile2 = homeaddress.Mobile2;
            c.Email1 = homeaddress.Email1;
            c.Email2 = homeaddress.Email2;

            
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.SD_WorkAddress.DeleteObject(db.SD_WorkAddress.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
